package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.AttendanceParams;
import com.isunland.managesystem.entity.CarUsedParams;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.FileListParams;
import com.isunland.managesystem.entity.LocaleWorkQueryCount;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能: 任务详情界面
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2016/12/12 19:50
 */

public class LocaleWorkQueryCountDetailFragment extends LocaleWorkQueryCountBaseFragment
    implements View.OnClickListener {

  public static LocaleWorkQueryCountDetailFragment newInstance(LocaleWorkQueryCount item) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_ITEM, item);
    LocaleWorkQueryCountDetailFragment fragment = new LocaleWorkQueryCountDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setTitleCustom(R.string.taskDetail);
    disableView();
    slDocBlob.setOnClickContentListener(this);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_locale_work, menu);
    if (mItem == null || mItem.getDataStatus() == null) {
      return;
    }
    String mStatus = mItem.getDataStatus();
    //除了审核通过都可以
    if (!mStatus.equals(DataStatus.PUBLIS)) {
      //修改,删除
      menu.getItem(0).setVisible(true);
      menu.getItem(1).setVisible(true);
      menu.getItem(2).setVisible(true);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    //if (item == null) return super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
      case R.id.menu_item_alter:
        LocaleWorkQueryCountUpdateActivity.newInstance(this, mActivity, mItem, REQUEST_ALTER);
        return true;
      case R.id.menu_item_delete:
        volleyPostDelete();
        return true;

      case R.id.menu_item_submit:
        volleyPostUpdateStatus(mId, TYPE_SUBMIT);
        return true;
      case R.id.menu_item_signCheck://出勤核查
        //需要日期不要时间的字符串.
        //noinspection deprecation
        //出勤核查,如果参与人为空则不跳转,并提示.
        if (MyStringUtil.isEmpty(slWorkernames.getTextContent())) {
          ToastUtil.show(MyStringUtil.connect(R.string.no1, R.string.workernames1));
          return false;
        }
        @SuppressWarnings("deprecation") String startDate = MyDateUtil.parseDateToString(
            MyDateUtil.parseStringToTime(slWorkstartdate.getTextContent()));
        //noinspection deprecation
        String endDate = MyDateUtil.parseDateToString(
            MyDateUtil.parseStringToTime(slWorkenddate.getTextContent()));
        AttendanceParams params = new AttendanceParams();
        params.setNames(slWorkernames.getTextContent());
        params.setJobNos(mWorkerIds);
        params.setStartDate(startDate);
        params.setEndDate(endDate);
        params.setTitle(getString(R.string.signCheck));
        params.setTypeFrom(AttendanceListFragment.TYPE_LOCALE_WORK);
        BaseVolleyActivity.newInstance(this, AttendanceListActivity.class, params, 0);
        return true;
      case R.id.menu_item_carUse:
        CarUsedParams carUsedParams = new CarUsedParams();
        carUsedParams.setMainId(mId);
        carUsedParams.setMainNames(CarUsedParams.TABLE_LOCALE_WORK);
        carUsedParams.setFrom(CarUsedParams.LOCALE_WORK);
        CarUsedListActivity.newInstance(this, mActivity, 0, carUsedParams);
        return true;
      //case R.id.menu_item_cropCheck://农资核查
      //  OperationSubLogListActivity.newInstance(this, mActivity, 0, mId, mDataStatus);
      //  return true;
      case R.id.menu_item_qualityCheck://质量核查
        QualityInspetionListActivity.newInstance(this, mActivity, 0, mId, mDataStatus);
        return true;
      case R.id.menu_item_taskAppoint://任务分配
        LocaleWorkQueryCountSubListActivity.newInstance(this, mActivity, 0, mId, mDataStatus,
            mItem);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void volleyPostDelete() {
    if (mItem == null) return;
    if (mItem.getId() == null) return;
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_DELETE_LOCALE_WORK);
    HashMap<String, String> params = new HashMap<>();
    if (mItem != null) params.put("id", mItem.getId());
    MyUtils.showDialogLoading(mActivity);
    mActivity.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
        ToastUtil.show(R.string.failure_operation);
      }

      @Override public void onVolleyResponse(String json) throws JSONException {
        //json = MyUtils.readAssetsFile(MyApplication.getAppContext(), "testOperationDetail.json");
        Base original = new Gson().fromJson(json, Base.class);
        if (original.getResult() == Base.SUCCESS) {
          ToastUtil.show("删除成功");
          mActivity.setResult(Activity.RESULT_OK);
          mActivity.finish();
        } else {
          ToastUtil.show("删除失败!");
        }
      }
    });
    setIfEditSuccess(true);
    mActivity.finish();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_ALTER) {
      volleyPostInit();
      setIfEditSuccess(true);
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  /**
   * 禁用输入和选择
   */
  private void disableView() {
    slWorktypeshowvalue.setInputEnabled(false);
    //slCropname.setInputEnabled(false);
    slWorkernames.setInputEnabled(false);
    slWorkstartdate.setInputEnabled(false);
    slWorkenddate.setInputEnabled(false);
    slPrice.setInputEnabled(false);
    slAreaWork.setInputEnabled(false);
    //slMedicineprice.setInputEnabled(false);
    //slDosage.setInputEnabled(false);
    slOtherCost.setInputEnabled(false);
    slTotalcost.setInputEnabled(false);
    slPayedcost.setInputEnabled(false);
    slUnpay.setInputEnabled(false);
    slRegStaffNameWork.setInputEnabled(false);
    slRegDateWork.setInputEnabled(false);
    slAreaname.setInputEnabled(false);
    slRegion.setInputEnabled(false);
    //slIsConfirmed.setInputEnabled(false);
    //slConfirmedArea.setInputEnabled(false);
    //slConnectedApplyer.setInputEnabled(false);
    slDataStatusName.setInputEnabled(false);
    slIspayName.setInputEnabled(false);
    slJoinStaffName.setInputEnabled(false);
    slDeviceNo.setInputEnabled(false);
    slDeviceName.setInputEnabled(false);
    slWeatherName.setInputEnabled(false);
    slWeatherInfo.setInputEnabled(false);
    slCropInfoNo.setInputEnabled(false);
    slCropInfoNum.setInputEnabled(false);
    slTechnicalParam.setInputEnabled(false);
    slLandDeepth.setInputEnabled(false);
    slWarrantyCost.setInputEnabled(false);
    slOilNum.setInputEnabled(false);
    slOilNo.setInputEnabled(false);
    slRepairUse.setInputEnabled(false);
    slWorkLocation.setInputEnabled(false);
    slOldWeight.setInputEnabled(false);
    slAfterProcessWeight.setInputEnabled(false);
    slElectricityNum.setInputEnabled(false);
    slWaterNum.setInputEnabled(false);
    slPickupLocation.setInputEnabled(false);
    slDeliveryLocation.setInputEnabled(false);
    slSoilInfo.setInputEnabled(false);
    slRemarkWork.setInputEnabled(false);
    //slDocBlob.setInputEnabled(false);
    slCheckStaffName.setInputEnabled(false);
    //slMedicinename.setInputEnabled(false);
  }

  @Override public void onClick(View v) {

    if (!(v.getTag() instanceof Integer)) return;
    int id = (int) v.getTag();
    switch (id) {
      case R.id.sl_docBlob:
        //if (TextUtils.isEmpty(mDocPath)) return;
        //ExtraDownLoadDialogFragment dialog = ExtraDownLoadDialogFragment.newInstance("", mDocPath);
        //FragmentManager fm = getActivity().getSupportFragmentManager();
        //dialog.show(fm, "");
        //相关附件子表
        FileListParams params = new FileListParams();
        params.setMainId(mId);
        BaseVolleyActivity.newInstance(this, FileListActivity.class, params, 0);
        break;
      default:
        super.onClick(v);
        break;
    }
  }
}
